jQuery(function(){
	jQuery.ajaxSetup({
		cache: false
	});
});

var ASURL = ctx+"/ajax/userAccountSettingsForm.html?type=";
var ASURLPart = ctx+"/ajax/userAccountSettingsPart.html?type=";
var $openPlaceHolder, $hiddenPanel;

function editAS(type){
	switch(type){
		case 1: 
			showEditBIForm();
			break;
		case 2:
			showEditCIForm();
			break;
		case 3: 
			showEditSnC();
			break;
		case 4: 
			showEditRtForm();
			break;
		case 5: 
			showEditHobbies();
			break;
		case 6:
			showEditPasswordForm();
			break;
	}
}

function cancelEditAS(prefix){
	$placeholder = jQuery("#"+prefix+"_placeholder");
	$panel = jQuery("#"+prefix);
	$placeholder.fadeOut();
	$panel.fadeIn();
}

function showEditBIForm(){
	var _url = ASURL + 1;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#bi_placeholder'); 
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#bi");
			jQuery("#bi").fadeOut();
		}
	});
}

function showEditCIForm(){
	var _url = ASURL + 2;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#ci_placeholder');
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#ci");
			jQuery("#ci").fadeOut();
		}
	});
}

function showEditSnC(){
	var _url = ASURL + 3;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#snc_placeholder');
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#snc");
			jQuery("#snc").fadeOut();
		}
	});
}

function showEditRtForm(){
	var _url = ASURL + 4;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#rt_placeholder');
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#rt");
			jQuery("#rt").fadeOut();
		}
	});
}

function showEditHobbies(){
	var _url = ASURL + 5;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#hb_placeholder');
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#hb");
			jQuery("#hb").fadeOut();
		}
	});
}

function showEditPasswordForm() {
	var _url = ASURL + 6;
	jQuery.ajax({
		url: _url,
		success: function(data) {
			var $placeholder = jQuery('#pw_placeholder');
			$openPlaceHolder = $placeholder;
			$placeholder.html(data).fadeIn();
			bindValidation();
		},
		beforeSend: function(){
			$hiddenPanel = jQuery("#pw");
			jQuery("#pw").fadeOut();
		}
	});
}

function updateBI(){
	var $placeholder = jQuery('#bi_placeholder');
	var bDate = $placeholder.find("#birthDate option:selected").val();
	var bMonth = $placeholder.find("#birthMonth option:selected").val();
	var bYear = $placeholder.find("#birthYear option:selected").val();
	var _fn = $placeholder.find("#firstName").val();
	var _mn = $placeholder.find("#middleName").val();
	var _ln = $placeholder.find("#lastName").val();
	var _em = $placeholder.find("#email").val();
	var _am = $placeholder.find("#aboutMe").val();
	var _male = $placeholder.find("#male").val();
	
	var bi = {
		firstName: _fn,
		middleName: _mn,
		lastName: _ln,
		birthdayDate: bDate,
		birthdayMonth: bMonth,
		birthdayYear: bYear,
		male: _male,
		email: _em,
		aboutMe: _am
	}
	UserManagerExt.updateUserBasicInformation(bi, function(re){
		if(re == 1){
			$placeholder.fadeOut().html("");
			var _url = ASURLPart + 1;
			jQuery.ajax({
				url: _url,
				success: function(data){
					jQuery("#bi").html(data).fadeIn();
				}
			})
		}		
	});
}

function updateCI(){
	var $placeholder = jQuery('#ci_placeholder');
	var _y = $placeholder.find("#yahoo").val();
	var _s = $placeholder.find("#skype").val();
	var _f = $placeholder.find("#facebook").val();
	var _m = $placeholder.find("#mobile").val();
	var _w = $placeholder.find("#website").val();
	
	var ci = {
		yahoo: _y,
		skype: _s,
		facebook: _f,
		mobile: _m,
		website: _w
	}
	UserManagerExt.updateUserContactInformation(ci, function(re){
		if(re == 1){
			$placeholder.fadeOut().html("");
			var _url = ASURLPart + 2;
			jQuery.ajax({
				url: _url,
				success: function(data){
					jQuery("#ci").html(data).fadeIn();
				}
			})
		}
	});
}

function updateSnC(){
	updateSchools();	
}

function updateSchools(){
	var $placeholder = jQuery('#snc_placeholder');
	var schools = new Array();
	jQuery(".accSettingRow[rel=school]").each(function(index){
		var $this = jQuery(this);
		var _id = $this.find(".school_id").val();
		var _name = $this.find(".school_name").val();
		var _mainStudy = $this.find(".school_main_study").val();
		var _title = $this.find(".school_title").val();
		var _from = $this.find(".school_year_from").val();
		var _to = $this.find(".school_year_to").val();
		var _cityId = $this.find(".school_city option:selected").val();
		var school = {
			id: _id,
			name: _name,
			mainStudy: _mainStudy,
			title: _title,
			from: _from,
			to: _to,
			cityId: _cityId
		}
		schools[index] = school;
	});
	
	UserManagerExt.updateUserSchools(schools, function(re){
		if(re == 1){
//			$placeholder.fadeOut().html("");
//			var _url = ASURLPart + 3;
//			jQuery.ajax({
//				url: _url,
//				success: function(data){
//					jQuery("#snc").html(data).fadeIn();					
//				}
//			})
			updateCompanies();
		}
	});
}

function updateCompanies(){
	var $placeholder = jQuery('#snc_placeholder');
	var companies = new Array();
	jQuery(".accSettingRow[rel=company]").each(function(index){
		var $this = jQuery(this);
		var _id = $this.find(".company_id").val();
		var _name = $this.find(".company_name").val();
		var _field = $this.find(".company_field").val();
		var _title = $this.find(".company_title").val();
		var _from = $this.find(".company_year_from").val();
		var _to = $this.find(".company_year_to").val();
		var _cityId = $this.find(".company_city option:selected").val();
		var company = {
			id: _id,
			name: _name,
			field: _field,
			title: _title,
			fromYear: _from,
			toYear: _to,
			cityId: _cityId
		}
		companies[index] = company;
	});
	
	UserManagerExt.updateUserCompanies(companies, function(re){
		if(re == 1){
			$placeholder.fadeOut().html("");
			var _url = ASURLPart + 3;
			jQuery.ajax({
				url: _url,
				success: function(data){
					jQuery("#snc").html(data).fadeIn();
				}
			})
		}
	});	
}

function updateRt(){
	var $placeholder = jQuery('#rt_placeholder');
	var _a = $placeholder.find("#address").val();
	var _d = $placeholder.find("#district").val();
	var _c = $placeholder.find("#city option:selected").val();
	var rt = {
		address: _a,
		district: _d,
		cityId: _c
	}
	UserManagerExt.updateUserResidentInformation(rt, function(re){
		if(re == 1){
			$placeholder.fadeOut().html("");
			var _url = ASURLPart + 4;
			jQuery.ajax({
				url: _url,
				success: function(data){
					jQuery("#rt").html(data).fadeIn();
				}
			})
		}
	});
}

function createHobbies(_category, values){
	var result = new Array();
	var val = values.split(",");
	for(var index=0; index<val.length; index++){
		var _name = val[index];
		result[index] = {category: _category, name: _name}
	}
	return result;
}

function updateHb(){
	var $placeholder = jQuery('#hb_placeholder');
	var _music = $placeholder.find("#musicFav").val();	
	var musics = createHobbies("music", _music);
	var _movie = $placeholder.find("#movieFav").val();
	var movies = createHobbies("movie", _movie);
	var _book = $placeholder.find("#bookFav").val();
	var books = createHobbies("book", _book);
	var _food = $placeholder.find("#foodFav").val();
	var foods = createHobbies("food", _food);
	
	var hobbies = musics.concat(movies, books, foods);
	UserManagerExt.updateUserHobbies(hobbies, function(re){
		if(re == 1){
			$placeholder.fadeOut().html("");
			var _url = ASURLPart + 5;
			jQuery.ajax({
				url: _url,
				success: function(data){
					jQuery("#hb").html(data).fadeIn();
				}
			})
		}
	});
	
}

function addCompany(){
	var $template = jQuery("#company_template");
	var $placeholder = jQuery("#company_placeholder");
	var $clone = $template.clone().removeAttr("id");
	$clone.insertBefore($placeholder).fadeIn();
}

function removeCompany(a){
	var $link = jQuery(a);
	$link.prev(".addDataButton").remove();
	$link.prev(".clearthis").remove();
	$link.prev(".accSettingRow").remove();
	$link.remove();
	
	if(jQuery(".accSettingRow[rel=company]").length <= 1){
		addCompany();
	}
}

function addSchool(){
	var $template = jQuery("#school_template");
	var $placeholder = jQuery("#school_placeholder");
	var $clone = $template.clone().removeAttr("id");
	$clone.insertBefore($placeholder).fadeIn();
}

function removeSchool(a){
	var $link = jQuery(a);
	$link.prev(".addDataButton").remove();
	$link.prev(".clearthis").remove();
	$link.prev(".accSettingRow").remove();
	$link.remove();
	
	if(jQuery(".accSettingRow[rel=school]").length <= 1){
		addSchool();
	}
}

function updatePassword() {
	// validate information
	var validation = true;
	validation = validation && validateOldPass();
	validation = validation && validateNewPass();
	validation = validation && validateConfirmPass();
	
	if (validation) {
		var oldPass = jQuery("#old_password_input").val();
		var newPass = jQuery("#password_input").val();
		UserManagerExt.updateUserPassword(oldPass, newPass, function(result) {
			if (result == 1) {
				cancelEditAS('pw');
			}
		});
	}
}

function bindValidation() {
	// CHANGE PASSWORD VALIDATION - BEGIN
	jQuery("#old_password_input").blur(function() {
		validateOldPass();
	});
	jQuery("#password_input").blur(function() {
		validateNewPass();
	});
	jQuery("#password_retype_input").blur(function() {
		validateConfirmPass();
	});
	// CHANGE PASSWORD VALIDATION - END
}

function validateOldPass() {
	var password = jQuery("#old_password_input").val();
	UserManagerExt.checkUserPassword(password, function(result) {
		if (!result) {
			jQuery("#old_password_input").focus();
			jQuery("#old_password_error").hide().show();
			setTimeout(function() { jQuery("#old_password_error").fadeOut(500); }, 3000);

			return false;
		}
	});
	return true;
}

function validateNewPass() {
	var pass = jQuery("#password_input").val();
	if (pass.length < 6) {
		jQuery("#password_input").focus();
		jQuery("#password_error").hide().show();
		setTimeout(function() { jQuery("#password_error").fadeOut(500); }, 3000);
		return false;
	}
	return true;
}

function validateConfirmPass() {
	var pass = jQuery("#password_input").val();
	var retype = jQuery("#password_retype_input").val();
	if (pass != retype) {
		jQuery("#password_retype_input").focus();
		jQuery("#password_retype_error").hide().show();
		setTimeout(function() { jQuery("#password_retype_error").fadeOut(500); }, 3000);
		return false;
	}
	return true;
}